package com.wfy.modules.openInterface.web;

import com.wfy.common.entity.Result;
import com.wfy.common.entity.ResultGenerate;
import com.wfy.common.service.ServiceException;
import com.wfy.common.util.PageData;
import com.wfy.common.web.BaseController;
import com.wfy.modules.openInterface.entity.DeviceInfo;
import com.wfy.modules.openInterface.service.DeviceInfoService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: wangxindong
 * Date: 2017/9/27
 * Time: 16:36
 */
@RestController
@RequestMapping(value = "/v1/device")
public class DeviceInfoController extends BaseController {

    // ================================================================
    // Constants
    // ================================================================

    // ================================================================
    // Fields
    // ================================================================

    @Resource
    private DeviceInfoService deviceInfoService;


    // ================================================================
    // Constructors
    // ================================================================

    // ================================================================
    // Methods from/for super Interfaces or SuperClass
    // ================================================================

    // ================================================================
    // Public or Protected Methods
    // ================================================================

    @ApiOperation(value = "装置列表")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "token", required = true, value = "平台颁发的KEY", dataType = "string", paramType = "header")
            }
    )
    @RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public Result list() {
        String token = getHttpTokenHeader();
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setToken(token);
        List<DeviceInfo> dl = deviceInfoService.findList(deviceInfo);
        return ResultGenerate.getSuccessResult(dl);
    }


    @ApiOperation(value = "根据装置编号，获取装置下所有传感器的实时数据")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "code", required = true, value = "装置编号", dataType = "string", paramType = "path"),
                    @ApiImplicitParam(name = "token", required = true, value = "平台颁发的KEY", dataType = "string", paramType = "header")
            }
    )
    @RequestMapping(value = "/{code}", method = RequestMethod.GET)
    @ResponseBody
    public Result get(@PathVariable String code, HttpServletRequest request) {
        String token = request.getHeader("token");
        PageData pd = getPageData();
        pd.put("code", code);
        pd.put("token", token);
        LOGGER.info(pd.toString());
        try {
            Result result = deviceInfoService.findDeviceSensorData(pd);
            return result;
        } catch (ServiceException e) {
            LOGGER.error(e.getMessage());
            return ResultGenerate.getFailResult();
        }
    }


    // ================================================================
    // Getter & Setter
    // ================================================================

    // ================================================================
    // Private Methods
    // ================================================================


    // ================================================================
    // Inner or Anonymous Class
    // ================================================================

    // ================================================================
    // Test Methods
    // ================================================================

}
